import React, {Component} from 'react'
import Forms from '@/components/common/Form'
import './css/basics.scss'
import { basicsEdit, getInfo } from '../../api/user'
import { getProductsList } from '../../api/products'
import { message} from 'antd';
export default class Layout extends Component {
    state = {
        formList: [
            // {
            //     label: 'logo',
            //     type: 'input',
            //     name: 'logo'
            // },
            {
                label: '电话',
                type: 'input',
                name: 'phone'
            },
            {
                label: '邮箱',
                type: 'input',
                name: 'email'
            },
            {
                label: '地址',
                type: 'input',
                name: 'address'
            },
            {
                label: 'qq',
                type: 'input',
                name: 'qq'
            },
            {
                label: 'logo图片',
                type: 'upload',
                name: 'logoImg'
            }
        ],
        test: '66',
        formData: {},
        logoImg: ''
    }
    formRef = React.createRef()
    componentDidMount() {
        this.getInfoFun()
    }
    getInfoFun = () => {
        const formData = {}
        getInfo().then(res => {
            console.log('res', res)
            for (const resKey in res[0]) {
                formData[resKey] = res[0][resKey]
            }
            formData.imgUrls = [formData.logoImg]
            this.formRef.current.setFieldsValue(formData, true)
        })

        getProductsList().then(res => {
            console.log('res', res)
        })

    }
    onClick = (data) => {
        let obj  = {}
        console.log('data', data)
        for (const k in data) {
            if (data[k]) obj[k] = data[k]
        }
        if (this.state.logoImg) {
            obj.logoImg = this.state.logoImg
        }
        basicsEdit(obj).then(res => {
            message.open({
                type: 'success',
                content: 'success',
            })
        })
    }
    render() {
        const { formList, formData } = this.state
        return (
            <div className="box">
                <Forms ref={this.formRef} formList={ formList } formData={ formData } onClick={ this.onClick } />
            </div>
        )
    }
}
